//
//  RZQCuriousViewController.m
//  LastOne
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQCuriousViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "CirleModel.h"
#import "RZQCuriousConllectionCell.h"
#import "RZQCuriousChooseViewController.h"
#import "RZQCuriousSayViewController.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@interface RZQCuriousViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong)UICollectionView *CuriousController;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, assign)CGFloat titleSize;
@property(nonatomic, assign)CGFloat messageSize;

@end

@implementation RZQCuriousViewController
#pragma mark 网络解析
-(void)getNet
{
    [GiFHUD show];
     _dataSource = [NSMutableArray array ];
    [HTTPMethod getDataByString:@"http://app.qdaily.com/app/papers/index/0.json" BodyDic:nil WithDataBlock:^(id data) {
       
        NSDictionary *dic = [data objectForKey:@"response"];
        NSDictionary *Dic = [dic objectForKey:@"feeds"];
        NSArray *array = [Dic objectForKey:@"list"];
        for (NSDictionary *dic  in array) {
            NSDictionary *post = [dic objectForKey:@"post"];
            CirleModel *model = [[CirleModel alloc]init];
            [model setValuesForKeysWithDictionary:post];
            [_dataSource addObject:model];
        }
        [_CuriousController reloadData];
        [GiFHUD dismiss];
    }];
 
}
-(CGFloat)heightAction: (NSString *)string With:(NSInteger)font
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font], NSFontAttributeName, nil];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(ScreenWidth - 10 * FitWidth , CGFLOAT_MAX * FitHeight ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    
    return rect.size.height;
    
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self getNet];
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init ];
    
    flowLayout.minimumLineSpacing = 0 * FitHeight;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0 * FitWidth, 5 * FitHeight, 0 * FitWidth);
    _CuriousController = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113 * FitHeight) collectionViewLayout:flowLayout];
    _CuriousController.delegate = self;
    _CuriousController.dataSource = self;
    _CuriousController.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_CuriousController];
    
    [_CuriousController registerClass:[RZQCuriousConllectionCell class] forCellWithReuseIdentifier:@"RZQCurious"];
   
    [_CuriousController NightWithType:UIViewColorTypeNormal];
    [GiFHUD setGifWithImageName:@"loading.gif"];
    // Do any additional setup after loading the view.
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    _titleSize   = [self heightAction:[_dataSource[indexPath.row]title] With:30];
    _messageSize = [self heightAction:[_dataSource[indexPath.row]Description] With:18];
   
    return CGSizeMake(ScreenWidth , 230 * FitHeight + _titleSize + _messageSize);
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZQCuriousConllectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RZQCurious" forIndexPath:indexPath];
    
    cell.curiouseData = _dataSource [indexPath.row];
    cell.titlesize = [self heightAction:[_dataSource[indexPath.row]title] With:30];
    cell.messagesize = [self heightAction:[_dataSource[indexPath.row]Description] With:18];

    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CirleModel *cir = _dataSource[indexPath.row];
    if ([[cir.category objectForKey:@"id"] isEqualToNumber:[NSNumber numberWithInteger:1]]) {
         self.hidesBottomBarWhenPushed = YES;
        RZQCuriousChooseViewController *rzq = [[RZQCuriousChooseViewController alloc]init];
        [self.navigationController pushViewController:rzq animated:YES];
        
        rzq.modelData = cir;
        self.hidesBottomBarWhenPushed = NO;
    }
    else
    {
        RZQCuriousSayViewController *rzq = [[RZQCuriousSayViewController alloc]init];
        [self.navigationController pushViewController:rzq animated:YES];
        
        rzq.modelData = cir;
        self.hidesBottomBarWhenPushed = NO;
    
    }
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
